package com.kajespero.webapp.controller.summary;

import java.io.IOException;
import java.io.Serializable;
import java.util.LinkedHashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.kajespero.model.SummaryBean;
import com.kajespero.service.summary.impl.SummaryServiceImpl;
import com.kajespero.webapp.AttributesConstants;
import com.kajespero.webapp.controller.commons.AbstractController;

@Controller
@RequestMapping("/summary")
public class SummaryController extends
		AbstractController<SummaryServiceImpl, SummaryBean> {

	private static final Log LOG = LogFactory.getLog(SummaryController.class);
	
	@Override
	@RequestMapping(value = "/create_summary")
	public ModelAndView create() {
		ModelAndView modelAndView = new ModelAndView("create_summary");
		SummaryBean summary = getService().createNewBean();
		modelAndView.addObject("summary", summary);
		modelAndView.addObject("ownerSummary", getService().findAll());
		return modelAndView;
	}

	@Override
	@RequestMapping(value = "/update_summary")
	public ModelAndView update(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("create_summary");
		Long summaryId = Long.decode(request.getParameter("id"));
		SummaryBean summary = getService().find(summaryId);
		modelAndView.addObject("summary", summary);
		modelAndView.addObject("ownerSummary", getService().findAll());
		return modelAndView;
	}

	@Override
	@RequestMapping(value = "/delete_summary", method = RequestMethod.POST)
	public void delete(HttpServletRequest request) {
		Long summaryId = Long.decode(request.getParameter("id"));
		getService().delete(summaryId);
	}

	@Override
	@RequestMapping(value = "/save_summary", method = RequestMethod.POST)
	public void saveOrUpdate(@ModelAttribute("summary")SummaryBean instance, BindingResult result, HttpServletRequest request,
			HttpServletResponse response)
			throws ServletException, IOException {
		SummaryBean oldSummary = getService().find(instance.getId());
		if (oldSummary != null) {
			instance.setKey(oldSummary.getKey());
		}
		getService().saveOrUpdate(instance);
		String path = "/administration";
		String req = response.encodeRedirectURL("/index?"
				+ AttributesConstants.PATH + "=" + path);
		try {
			response.sendRedirect(req);
		} catch (IOException e) {
			LOG.error("IOException " + e.getMessage(), e);
		}
	}

	@RequestMapping(value = "/rest/getAllSummary", method= RequestMethod.POST)
	public ModelAndView getAllSummary(@RequestBody LinkedHashMap<String, Serializable> map){
		ModelAndView modelAndView = new ModelAndView();
		if(checkCredential(map)){
			modelAndView.addObject("summaries", getService().findAll());
		}
		return modelAndView;
	}
	
	@RequestMapping(value="/rest/saveSummary", method=RequestMethod.POST)
	public void saveOrUpdate(@RequestBody LinkedHashMap<String, Serializable> map){
		if(checkCredential(map)){
			// TODO A completer 
		}
	}
}
